﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CasaBella.BL;
using CasaBella.Entities;
using Microsoft.Practices.Unity;

namespace CasaBella.UI
{
    public partial class FrmMttoSexo : Form
    {
        [Dependency]
        public ISexoBL sexoBL { get; set; }

        public FrmMttoSexo()
        {
            InitializeComponent();
            this.BuilUp();
        }

        bool isNew;

        public void NuevoSexo()
        {
            isNew = true;

            sEXOBindingSource.AddNew();
        }

        public SEXO _sexo { get; set; }

        public void EditarSexo()
        {
            isNew = false;

            sEXOBindingSource.DataSource = _sexo;
        }

        private void FrmMttoSexo_Load(object sender, EventArgs e)
        {

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var sexo = sEXOBindingSource.Current as SEXO;

            var msg = "ERROR";

            if (sexo != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        sexo.Descripcion = descripcionTextBox.Text.ToUpper();
                        sexoBL.AddSexo(sexo);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        sexo.Descripcion = descripcionTextBox.Text.ToUpper();
                        sexoBL.UpdateSexo(sexo);
                        MessageBox.Show("Modificación Correcta", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
